using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Keep.Const;
using Keep.Model;
using Keep.Util;

namespace Keep.Service.Impl;

public class LinkTypeServiceImpl : ILinkTypeService
{
    public bool AddLinkType(LinkType linkType)
    {
        linkType.num = 0;
        linkType.status = Static.Enable;
        int i = SqlSugarHelper.Db.Insertable(linkType).ExecuteCommand();
        return i > 0;
    }

    public void AddLinkTypeClick(int id)
    {
        SqlSugarHelper.Db.Updateable<LinkType>()
            .SetColumns(it => it.num == it.num + 1)
            .Where(it => it.id == id)
            .ExecuteCommand();
    }

    public bool DelLinkType(int id)
    {
        int i = SqlSugarHelper.Db.Updateable<LinkType>()
                .SetColumns(it => it.status == Static.Disable)
                .Where(lt => lt.id == id)
                .ExecuteCommand();
        return i > 0;
    }

    public bool EditLinkType(LinkType linkType)
    {
        int i = SqlSugarHelper.Db.Updateable<LinkType>()
                .SetColumnsIF(!String.IsNullOrEmpty(linkType.name), it => it.name == linkType.name)
                .Where(lt => lt.id == linkType.id)
                .ExecuteCommand();
        return i > 0;
    }

    public List<LinkType> SelLinkType()
    {
        var query = SqlSugarHelper.Db.Queryable<LinkType>();
        query.Where(lt => lt.status.Equals(Static.Enable));
        query.OrderByDescending(lt => lt.num);
        return query.ToList();
    }
}
